from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep
from selenium.webdriver.common.by import By
import os
import json


chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
# chrome_options.add_argument('--kiosk')
chrome_options.add_argument('disable_infobars')

chrome_options.add_experimental_option(
    'prefs', {'download.prompt_for_download': True})

# 谷歌驱动 告诉电脑在哪打开浏览器
bw = webdriver.Chrome("chromedriver", options=chrome_options)


pages = ['http://www.uooc.net.cn/home/course/1120903539#/test',
         'http://www.uooc.net.cn/home/course/1120903539#/homework']

bw.get(pages[0])

print("拉起登录")

sleep(20)


chapters = []


def try_run(callback: callable):
    try:
        return callback()
    except Exception as e:
        print(e.args[0])
        return None


def text(el, default: any = None) -> str:
    if el is None:
        return default
    result = el.get_attribute("innerText")
    if result == None:
        return default
    return result


def finds(selector: str, parent=None):
    if (parent is None):
        parent = bw
    return parent.find_elements(By.CSS_SELECTOR, selector)


def find(selector: str, parent=None):
    if (parent is None):
        parent = bw
    return parent.find_element(By.CSS_SELECTOR, selector)

# 拍平数据


def flat_list(arr, result=[]):
    if not isinstance(arr, list):
        return result
    for ele in arr:
        if isinstance(ele, list):
            flat_list(ele, result)
        else:
            result.append(ele)
    return result


def fetch_all_chapters():
    result = []
    eles = finds('.table-hw tbody tr.ng-scope a')

    for chapter in eles:
        to = try_run(lambda: chapter.get_attribute("href"))
        if to is None:
            continue
        result.append(to)
    return result


def fetch_chapter_quetion(chapter: (str, str)) -> dict:
    result = []
    for quetion in finds('.queContainer'):
        right = text(
            find('.answerBox [ng-if="question.answer.length>0"] div', quetion))
        options = finds('.ti-a', quetion)
        title = text(find('.ti-q-c', quetion))
        item = dict()
        item['title'] = title.strip()
        item['right'] = right.strip()
        item['options'] = list(map(lambda el: (text(
            find('.ti-a-i', el))[:1].strip(), text(find('.ti-a-c', el)).strip()), options))
        result.append(item)
    return result


def fetch_chapters_quetion(chapters: list((str, str))):
    print("共 %d 个页面" % len(chapters))
    result = []
    for to in chapters:
        print("加载 %s" % to)
        bw.get(to)
        bw.implicitly_wait(30)
        print("解析 %s 页面" % to)
        item = fetch_chapter_quetion(to)
        result.append(item)
        sleep(2)

    with open(os.path.join(os.getcwd(), 'public/data1.txt'), 'w', encoding="utf-8") as fp:
        fp.write(json.dumps(result, indent=None, ensure_ascii=True))


def fetch_all_page():
    result = []
    for page in pages:
        print("获取 %s 页面下的目录" % page)
        bw.get(page)
        bw.implicitly_wait(30)
        sleep(2)
        result.append(fetch_all_chapters())
        print("共 %d 条数据" % len(result[len(result) - 1]))
        sleep(2)
    return flat_list(result)


focus_chapters = ['http://www.uooc.net.cn/exam/paper?cid=1120903539&tid=671214417', 'http://www.uooc.net.cn/exam/paper?cid=1120903539&tid=1826379927',
                  'http://www.uooc.net.cn/exam/paper?cid=1120903539&tid=1116336584', 'http://www.uooc.net.cn/exam/paper?cid=1120903539&tid=19148558', 'http://www.uooc.net.cn/exam/paper?cid=1120903539&tid=1265898883', 'http://www.uooc.net.cn/exam/paper?cid=1120903539&tid=1741557957', 'http://www.uooc.net.cn/exam/paper?cid=1120903539&tid=539682990', 'http://www.uooc.net.cn/exam/paper?cid=1120903539&tid=1520612827']

# 获取数据
fetch_chapters_quetion(focus_chapters)

bw.close()
